<template>
  <div class="productdetail">
    <el-container>
      <el-header>
        <el-row>
          <el-col :span="2"
            ><div class="grid-content bg-purple">
              {{ User.username }},欢迎你
            </div></el-col
          >
        </el-row>
      </el-header>
      <el-main>
        <el-image
          :src="require(`@/assets/image/p${product.id}.png`)"
          :fit="fill"
        ></el-image>
        <div class="text_operate">
          <p class="name">{{ product.name }}</p>
          <p class="description">产品描述：{{ product.description }}</p>
          <p class="price">¥{{ product.price }}</p>
          <p class="text_2">保障：7天无理由退货 极速退款</p>
          <div class="num">
            数量：<el-input-number
            v-model="product_num"
            @change="handleChange"
            :min="1"
            :max="10"
            label="描述文字"
          ></el-input-number>
          </div>
          <el-button style="width:200px" type="primary" @click="addCart">添加购物车</el-button>
        </div>
      </el-main>
    </el-container>
  </div>
</template>

<script>
import axios from "axios";
export default {
  data() {
    return {
      product: null,
      User: JSON.parse(sessionStorage.getItem("User")),
      product_num: 1,
    };
  },
  created() {
    const queryParams = this.$route.query.data;
    if (queryParams) {
      this.product = JSON.parse(decodeURIComponent(queryParams));
    }
  },
  methods: {
    handleChange(value) {
      console.log(value);
    },
    addCart(){
      let data = {
        username: this.User.username,
        num:this.product_num,
        productId:this.product.id
      };
      // 设置axios请求的headers，指定内容类型为application/json
      const config = {
        headers: {
          "Content-Type": "application/json",
        },
      };
      axios
        .post("http://localhost:8080/users/addCart", JSON.stringify(data), config)
        .then((response) => {
          alert("添加成功");
        })
        .catch((error) => {
          console.error("请求错误:", error);
          alert(error.response.data.message);
        });
    }
  },
};
</script>

<style scoped>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  text-align: center;
  line-height: 60px;
}
.el-main {
  background-color: #e9eef3;
  color: #333;
  display: flex; /* 使用flexbox布局 */
  align-items: flex-start; /* 垂直对齐顶部 */
}
.el-container {
  height: 100vh;
}
.el-image {
  width: 500px;
  height: 500px;
  margin-left: 15%;
  margin-top: 100px;
}
.text_operate {
  margin-top: 95px;
  margin-left: 100px;
}
.name {
  margin-bottom: 55px;
  color: #11192d;
  font-family: PingFangSC-Semibold;
  font-size: 20px;
  font-weight: 600;
}
.description{
  margin-bottom: 55px;
  color: #11192d;
  font-family: PingFangSC-Semibold;
  font-size: 20px;
  font-weight: 600;
}
.price {
  margin-bottom: 55px;
  color: rgb(255, 80, 0);
  font-size: xx-large;
}
.text_2 {
  margin-bottom: 55px;
  font-family: PingFangSC-Regular;
  font-size: large;
}
.num{
   margin-bottom: 55px;
}
</style>

